package org.dromara.netbar.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.json.JSONObject;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.netbar.domain.bo.NetbarMemberLevelBo;
import org.dromara.netbar.domain.vo.NetbarMemberLevelVo;
import org.dromara.netbar.service.INetbarCouponService;
import org.dromara.netbar.service.INetbarMemberLevelService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.netbar.domain.vo.NetbarSeatTypeVo;
import org.dromara.netbar.domain.bo.NetbarSeatTypeBo;
import org.dromara.netbar.service.INetbarSeatTypeService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 座位类型管理
 *
 * @author Lion Li
 * @date 2025-07-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/netbar/seatType")
public class NetbarSeatTypeController extends BaseController {

    private final INetbarSeatTypeService netbarSeatTypeService;

    private final INetbarMemberLevelService netbarMemberLevelService;

    private final INetbarCouponService netbarCouponService;
    /**
     * 查询座位类型管理列表
     */
    @SaCheckPermission("netbar:seatType:list")
    @GetMapping("/list")
    public TableDataInfo<JSONObject> list(NetbarSeatTypeBo bo, PageQuery pageQuery) {
        return netbarSeatTypeService.queryPageList2(bo, pageQuery);
    }

    /**
     * 导出座位类型管理列表
     */
    @SaCheckPermission("netbar:seatType:export")
    @Log(title = "座位类型管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(NetbarSeatTypeBo bo, HttpServletResponse response) {
        List<NetbarSeatTypeVo> list = netbarSeatTypeService.queryList(bo);
        ExcelUtil.exportExcel(list, "座位类型管理", NetbarSeatTypeVo.class, response);
    }

    /**
     * 获取座位类型管理详细信息
     *
     * @param seatTypeId 主键
     */
    @SaCheckPermission("netbar:seatType:query")
    @GetMapping("/{seatTypeId}")
    public R<NetbarSeatTypeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long seatTypeId) {
        return R.ok(netbarSeatTypeService.queryById(seatTypeId));
    }

    /**
     * 新增座位类型管理
     */
    @SaCheckPermission("netbar:seatType:add")
    @Log(title = "座位类型管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<?> add(@Validated(AddGroup.class) @RequestBody Map<String,Object> bo) {
        String seatTypeName = (String) bo.get("座位类型");
        if(seatTypeName == null){
            return R.fail("请填写座位类型");
        }
        String seatCode = (String) bo.get("座位代号");
        if(seatCode == null){
            return R.fail("请填写座位代号");
        }
        if (!bo.containsKey("基础费率")) {
            return R.fail("请填写基础费率");
        }
        String baseRate =(String) bo.get("基础费率");
        if(StringUtils.isBlank(baseRate)){
            return R.fail("请填写基础费率");
        }
        return netbarSeatTypeService.saveOrUpdate(bo);
    }

    /**
     * 删除座位类型管理
     *
     * @param seatTypeIds 主键串
     */
    @SaCheckPermission("netbar:seatType:remove")
    @Log(title = "座位类型管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{seatTypeIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] seatTypeIds) {
        Map<Long,Integer> countMap = netbarCouponService.countMapBySeatTypeIds(seatTypeIds);
        if (null!= countMap && !countMap.keySet().isEmpty()) {
            return R.fail("有卡券关联此座位类型，请先删除卡券");
        }
        return toAjax(netbarSeatTypeService.deleteWithValidByIds(List.of(seatTypeIds), true));
    }

    @SaCheckPermission("netbar:seatType:list")
    @GetMapping("/head/keys")
    public R<List<String>> getHeadKeys() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        List<String> headKeys = netbarSeatTypeService.getHeadKeys(loginUser.getTenantId());
        List<String> heads = new ArrayList<>(Arrays.asList("座位类型", "座位代号", "基础费率"));
        heads.addAll(headKeys);
        return R.ok(heads);
    }

    @SaCheckPermission("netbar:seatType:list")
    @GetMapping("/head/values")
    public R<List<Map<String,Object>>> getHeadValues() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        return R.ok(netbarSeatTypeService.getHeadValues(loginUser.getTenantId()));
    }

    @GetMapping("/data/all")
    public R<List<NetbarSeatTypeVo>> getAll() {
        return R.ok(netbarSeatTypeService.getSeatTypeVos());
    }

    @SaCheckPermission("netbar:seatType:edit")
    @Log(title = "座位类型管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<?> edit(@RequestBody Map<String,Object> bo) {
        Long seatTypeId = Long.parseLong((String) bo.get("座位Id"));
        bo.put("座位Id",seatTypeId);
        if(seatTypeId == null){
            return R.fail("请填写座位类型Id");
        }
        String seatTypeName = (String) bo.get("座位类型");
        if(seatTypeName == null){
            return R.fail("请填写座位类型");
        }
        String seatCode = (String) bo.get("座位代号");
        if(seatCode == null){
            return R.fail("请填写座位代号");
        }
        if (!bo.containsKey("基础费率")) {
            return R.fail("请填写基础费率");
        }
        String baseRate =(String) bo.get("基础费率");
        if(StringUtils.isBlank(baseRate)){
            return R.fail("请填写基础费率");
        }
        return netbarSeatTypeService.saveOrUpdate(bo);
    }
}
